/**
 * Copyright 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.examples.enhancedads.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

/**
 * AdGroup2ProdId maps the information for each product into the local
 * database.
 * 
 * @author rodrigo.f.vale@gmail.com
 */
public class AdGroup2ProdId {
  private Connection conn;
  private PreparedStatement update;
  private static final Logger log = Logger.getLogger("StarterKit");
  
  public AdGroup2ProdId(Connection conn) {
    this.conn = conn;
    Statement stat;
    try {
      stat = conn.createStatement();
      stat.executeUpdate(
          "CREATE TABLE IF NOT EXISTS AdGroup2ProdId "
        + "  ( "
        + "     AdGroupLocalId  INTEGER, "       
        + "     ProdId          STRING, "
        + "     AdParam1        STRING, "
        + "     AdParam1Min     FLOAT, "
        + "     AdParam1Max     FLOAT, "
        + "     AdParam2        STRING, "
        + "     AdParam2Min     FLOAT, "
        + "     AdParam2Max     FLOAT, "
        + "     PRIMARY KEY (AdGroupLocalId)"
        + "  ); ");

      update = conn.prepareStatement(
          "REPLACE INTO AdGroup2ProdId (AdGroupLocalId, ProdId, " +
          "AdParam1, AdParam1Min, AdParam1Max, AdParam2, AdParam2Min, " +
          "AdParam2Max) " +
          "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");      
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public void clear() {
    Statement stat;
    try {
      stat = conn.createStatement();
      stat.executeUpdate("DELETE FROM AdGroup2ProdId");                        
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public void saveBatch(int adGroupLocalId, String prodId, String adParam1,
      float adParam1Min, float adParam1Max, String adParam2,
      float adParam2Min, float adParam2Max) {
    try {
      update.setInt(1, adGroupLocalId);
      update.setString(2, prodId);
      update.setString(3, adParam1);
      update.setFloat(4, adParam1Min);
      update.setFloat(5, adParam1Max);
      update.setString(6, adParam2);
      update.setFloat(7, adParam2Min);
      update.setFloat(8, adParam2Max);
      update.addBatch();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public void executeBatch() {
    log.debug("Executing batch at AdGroup2ProdId");
    try {
      update.executeBatch();                        
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
}
